<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- <h2>公司设置</h2> -->
      <!-- 放置内容 -->
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 左侧信息 -->
            <el-row style="height:60px;">
              <el-button
                icon="el-icon-plus"
                type="primary"
                size="small"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <el-table border :data="list">
              <el-table-column type="index" align="center" label="序号" width="120"/>
              <el-table-column prop="name" align="center" label="名称" width="240"/>
              <el-table-column prop="description" align="center" label="描述"/>
              <el-table-column label="操作" align="center">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" style="height:60px;" align="middle">
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev,pager,next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 并不是所有的表单都需要model rules -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px"/>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px"/>
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="formData.companyPhone" disabled style="width:400px"/>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px"/>
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 放置一个弹层组件 -->
    <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
      <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name"/>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description"/>
        </el-form-item>
      </el-form>
      <!-- footer插槽 -->
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 放置弹层 -->
    <el-dialog title="分配权限" :visible="showPreDialog" @close="btnPreCancel">
      <!-- check-strictly 如果为true 表示父子勾选不互相关联  若为false 父子互相关联 -->
      <el-tree
        ref="premRef"
        :data="permData"
        :props="defaultProps"
        :show-checkbox="true"
        :check-strictly="true"
        :default-expand-all="true"
        :default-checked-keys="selectCheck"
        node-key="id"
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPreOK">确定</el-button>
          <el-button size="small" @click="btnPreCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  getRoleDetail,
  updateRole,
  addRole,
  assignPerm
} from '@/api/setting'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/premission'
import { transListToTreeDate } from '@/utils'
export default {
  data() {
    return {
      selectCheck: [], // 勾选权限的唯一标识
      permData: [], // 专门接收权限数据
      defaultProps: {
        label: 'name'
      }, // 定义显示字段的名称
      list: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 3,
        total: 0 // 记录总数
      },
      formData: {
        // 公司信息
      },
      showDialog: false,
      roleForm: {},
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      showPreDialog: false,
      roleId: null
    }
  },
  computed: {
    ...mapGetters(['companyId']),

    showTitle() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo() // 获取公司信息
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    changePage(newPage) {
      // newpage 是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前最新页码
      this.getRoleList() // 获取最新角色
    },
    // 删除所选id角色
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除该角色吗')
        await deleteRole(id) // 调用删除接口
        this.getRoleList() // 重新加载数据
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑弹层
    async editRole(id) {
      this.roleForm = await getRoleDetail(id) // 实现数据回写
      this.showDialog = true
    },
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()
        // 只有校验通过的情况下，才会执行await的下方内容
        // roleForm这个对象有id 就是编辑
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
        } else {
          // 新增
          await addRole(this.roleForm)
        }

        // 重置数据
        this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false // 关闭弹层 =》触发el-dialog 的close事件  间接调用
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 弹出层
    async assignPerm(id) {
      this.permData = transListToTreeDate(await getPermissionList(), '0') // 获取所有权限点
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      // console.log(permIds)
      this.selectCheck = permIds
      this.showPreDialog = true
    },
    async btnPreOK() {
      // getCheckedKeys()选中节点的key的数组
      await assignPerm({
        permIds: this.$refs.premRef.getCheckedKeys(),
        id: this.roleId
      })
      this.$message.success('分配权限成功')
      this.showPreDialog = false
    },
    btnPreCancel() {
      this.selectCheck = []
      this.showPreDialog = false
    }
  }
}
</script>

<style>
</style>
